$(function(){
    $("#login").click(function(){
        $("#count_span").html("")
        $("#password_span").html("")
        var name = $("#count").val().trim();
        var password = $("#password").val().trim();
        //console.log(name,password)
        //参数校验
        var ok = true;
        if(name == ""){
            ok = false;
            $("#count_span").html("用户名为空")
        }
        if(password == ""){
            ok = false;
            $("#password_span").html("密码为空")
        }
        //发送ajax请求
        if(ok){
            //通过校验
            $.ajax({
                url:"http://localhost:80/user/login.do",
                type:"post",
                dataType:"json",
                data:{"name":name,"password":password},
                success:function(result){
                    //status 0 1 2
                    if(result.status == 0){
                        //将用户id存放到cookie
                        var userId = result.data;
                        addCookie("userId",userId,0.5);
                        window.location.href="edit.html";
                    }else if(result.status == 1){
                        $("#count_span").html("用户名错误")
                    }else if(result.status == 2){
                        $("#password_span").html("密码错误")
                    }
                },
                error:function (XMLHttpRequest,textStatus,errorThrown) {
                    alert(XMLHttpRequest);
                    alert(textStatus);
                    alert(errorThrown);
                    alert("登录失败，稍后重试")
                }
            })
        }

    })
})